<?php
/*
 * Plugin Name: RSS and Twitter Counter
 * Plugin URI: http://www.gazpo.com
 * Description: A widget to show Feedburner subscribers and twitter followers.
 * Version: 1.0
 * Author: Sami Ch.
 * Author URI: http://gazpo.com
 */

add_action( 'widgets_init', 'gazpo_subscribers_count_widgets' );

function gazpo_subscribers_count_widgets() {
	register_widget( 'gazpo_subscribers_count_widget' );
}

class gazpo_subscribers_count_widget extends WP_Widget {


	function gazpo_subscribers_count_widget() {
		$widget_ops = array( 'classname' => 'widget_subscribers', 'description' => __('A widget to show Feedburner subscribers and twitter followers.', 'gazpo') );
		$this->WP_Widget( 'gazpo_subscribers_count_widget', __('RSS and Twitter Counter', 'gazpo'), $widget_ops );
	}
	
	function getTwitFeedburnCount($username, $type){
			if($type == "feedburner"):
				$cfile = sys_get_temp_dir().'/e1z'. $type . md5 ( $username );
			elseif($type == "twitter"):
				$cfile = sys_get_temp_dir().'/e1z'. $type . md5 ( $username );            
			endif;

			if (is_file ( $cfile ) == false):
				$cfile_time = strtotime ( '1983-04-30 07:15' );
			else:
				$cfile_time = filemtime ( $cfile );
			endif; 

			$difference = strtotime ( date ( 'Y-m-d H:i:s' ) ) - $cfile_time;

			if ($difference >= 3600):   // set the interval before updating the cache
			
			if($type == "feedburner"):
                $date = date('Y-m-d', strtotime('-4 days'));
				$data = file_get_contents("https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$username."&dates=".$date.",".$date);         
				$xml = new SimpleXMLElement($data);
				$total = $xml->feed->entry['circulation'];  //Read the total count from feedburner

			elseif($type == "twitter"):
				$data = file_get_contents ( 'http://twitter.com/users/show/' . $username );
				$xml = new SimpleXMLElement ( $data );
				$total = $xml->followers_count; // Read total follower count from twitters
            
			endif;

			if (is_file ( $cfile ) == true):
				unlink ( $cfile );
			endif;
				touch ( $cfile );
				file_put_contents ( $cfile, strval ( $total ) );
				return strval ( $total );
			else:
				$total = file_get_contents ( $cfile );
				return strval ( $total );
			endif;    
		}
		
	function widget( $args, $instance ) {
		extract( $args );
		$feedburner_id = $instance['feedburner_id'];
		$twitter_id = $instance['twitter_id'];

		echo $before_widget;
		$feedburner_count = $this -> getTwitFeedburnCount($feedburner_id, "feedburner");  
		$twitter_count = $this -> getTwitFeedburnCount($twitter_id, "twitter");  ;		
		?>
		
		<div class="widget_social_count">
			<ul>				
				<li class="rss">
					<a href="http://feeds.feedburner.com/<?php echo $feedburner_id; ?>"><?php echo $feedburner_count; ?></a><br>
					<span class="small">subscribers</span>
				</li>
				
				<li class="twitter">
					<a href="http://www.twitter.com/<?php echo $twitter_id; ?>"><?php echo $twitter_count; ?></a><br>
					<span class="small">followers</span>
				</li>
			</ul>
		</div>
		
        <?php
		echo $after_widget;
	}
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['feedburner_id'] = $new_instance['feedburner_id'];
		$instance['twitter_id'] = $new_instance['twitter_id'];
		return $instance;
	}

	function form( $instance ) {
	
		$defaults = array(
		'feedburner_id' => "gazpo",
		'twitter_id' => 'gazpodotcom'
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<p>
			<label for="<?php echo $this->get_field_id( 'feedburner_id' ); ?>"><?php _e('Feedburner ID:', 'gazpo') ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'feedburner_id' ); ?>" name="<?php echo $this->get_field_name( 'feedburner_id' ); ?>" value="<?php echo $instance['feedburner_id']; ?>" />
		</p>
        
       	<p>
			<label for="<?php echo $this->get_field_id( 'twitter_id' ); ?>"><?php _e('Twitter ID:', 'gazpo') ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'twitter_id' ); ?>" name="<?php echo $this->get_field_name( 'twitter_id' ); ?>" value="<?php echo $instance['twitter_id']; ?>" />
		</p>		
	<?php
	}
}
?>